#include "Wall.h"

const double PI = std::atan(1.0)*4;
const float white[] = {1.0,1.0,1.0,1.0};
const float black[] = {0.0,0.0,0.0,1.0};
const float red[] = {1.0,0.0,0.0,1.0};
const float green[] = {0.0,1.0,0.0,1.0};
const float blue[] = {0.0,0.0,1.0,1.0};
const float bluelight[] = {0.0,0.5,1.0,1.0};

const float yellow[] = {1.0,1.0,0.0,1.0};

const float yellowlight[] = {1.0,0.8,0.0,1.0};
const float yellowdark[] = {0.9,0.5,0.1,1.0};


//constructeur pour un mur traversable
Wall::Wall(void)
{
	twisted = true;
}

//constructeur pour un mur non traversable
Wall::Wall(int x,int z,int nbside,int angle){
	this->x = x;
	this->z = z;
	this->numberSharedSide = nbside;
	this->angle = angle;
	this->twisted = false;
}


Wall::~Wall(void)
{
}

void Wall::init(){

//   glNewList(TORUS, GL_COMPILE);
//      drawTorus(0.5f, 0.05f, 50, 50,360);
//   glEndList();

   glNewList(QUARTER_TORUS, GL_COMPILE);
      drawTorus(0.5f, 0.05f, 50, 50,90);
   glEndList();

   glNewList(CYLINDER, GL_COMPILE);
   	GLUquadricObj* quadric = gluNewQuadric();
      gluCylinder(quadric,0.05,0.05,1,20,20);
   glEndList();

//   glNewList(HALF_CYLINDER, GL_COMPILE);
//   	GLUquadricObj* quadric2 = gluNewQuadric();
//      gluCylinder(quadric2,0.05,0.05,0.5,20,20);
//   glEndList();

}

void Wall :: display(){

	if(!twisted)
	{
		glColor3f(0,0,1);
		glEnable(GL_COLOR_MATERIAL);
		GLfloat diffuse[] = { 0,0,1,1.0 };
		GLfloat specular[] = { 1.0,1.0,1.0,1.0 };
		GLfloat shinines[] = { 50.0 };
		GLfloat l_pos[] = { 1.0,1.0,1.0,0.0 };
		glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);
		glMaterialfv(GL_FRONT,GL_SPECULAR,specular);
		glMaterialfv(GL_FRONT,GL_SHININESS,shinines);

		glPushMatrix();

		if(angle==0)
			glTranslatef(x,0.,z-0.5);
		else{
			glTranslatef(x-0.5,0.,z);
			glRotatef(angle,0,1,0);
		}

		switch(numberSharedSide)
		{
		case(0) : createWall0();
			break;
		case(1) : createWall1();
			break;
		case(2) : createWall2();
			break;
		case(3) : createWall3();
			break;
		case(4) : createWall4();
			break;
		case(5) : createWall5();
			break;
		}
		glPopMatrix();
	}
}


void  Wall :: createWall0(){

}

void  Wall :: createWall1(){
//   printf("angle : %d \n",angle);
   glPushMatrix();

   switch(angle){

      case 0:
         glPushMatrix();
            glutSolidSphere(2,20,20);
         glPopMatrix();
      break;

      case 90:
         glPushMatrix();
            glColor3fv(white);

            glTranslatef(0,-0.6,1);
            glRotatef(90,0,1,0);
            glRotatef(-5,0,0,1);
            drawTorus(0.8f, 0.05f, 50, 50,90);
            drawTorus(0.4f, 0.05f, 50, 50,90);
         glPopMatrix();
      break;

      case 180:
      break;
    case 270:
      glPushMatrix();
         glColor3fv(white);
            glTranslatef(0,-0.6,0);
            glRotatef(90,0,1,0);
            glRotatef(-5,0,0,1);
            drawTorus(0.8f, 0.05f, 50, 50,90);
            drawTorus(0.4f, 0.05f, 50, 50,90);
      glPopMatrix();
   break;
   }
	glPopMatrix();

}

void Wall :: createWall2(){

	glPushMatrix();
      glTranslatef(0,-0.2,0);
      glCallList (CYLINDER);
   glPopMatrix();
   glPushMatrix();
      glTranslatef(0,0.2,0);
      glCallList (CYLINDER);
   glPopMatrix();
}

void  Wall :: createWall3(){

}

void Wall :: createWall4(){

	glPushMatrix();
   glColor3fv(red);

//	glTranslatef(0,0,0.5);
	glCallList (CYLINDER);

//	glTranslatef(x,0,z);
//	glRotatef(90,0,1,0);
//	glTranslatef(-0.5,0,-0.5);
	glCallList (CYLINDER);
	glPopMatrix();
}

//creation d'un coude
void Wall :: createWall5(){

   switch(angle){
      case 90:
         glPushMatrix();
            glColor3fv(bluelight);
            glTranslatef(.5,0.2,1.);
            glRotatef(-90,1,0,0);
            glRotatef(90,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();

         glPushMatrix();
            glTranslatef(.5,-0.2,1.);
            glRotatef(-90,1,0,0);
            glRotatef(90,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();
      break;
      case 180 :
         glPushMatrix();
            glColor3fv(bluelight);
            glTranslatef(0.,0.2,0.5);
            glRotatef(90,1,0,0);
            glRotatef(180,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();

         glPushMatrix();
            glTranslatef(0.,-0.2,0.5);
            glRotatef(90,1,0,0);
            glRotatef(180,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();
      break;
      case 270 :
         glPushMatrix();
            glColor3fv(bluelight);
            glTranslatef(0.5,0.2,0.);
            glRotatef(90,1,0,0);
            glRotatef(180,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();

         glPushMatrix();
            glTranslatef(0.5,-0.2,0.);
            glRotatef(90,1,0,0);
            glRotatef(180,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();
      break;
      case 0 :
         glPushMatrix();
            glColor3fv(bluelight);
            glTranslatef(0.5,0.2,1);
            glRotatef(90,1,0,0);
            glRotatef(180,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();

         glPushMatrix();
            glTranslatef(0.5,-0.2,1);
            glRotatef(90,1,0,0);
            glRotatef(180,0,0,1);
            glCallList (QUARTER_TORUS);
         glPopMatrix();
      break;
   }
}

bool Wall :: isTwisted(){
	return twisted;
}
